package edu.tongji.engine.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


public class PropertyUtil
{
    private Properties propertie;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;
    
    
    public PropertyUtil()
    {
        propertie = new Properties();
    }
    
  
    /**
     * @param filePath :property file path
     */
    public PropertyUtil(String filePath)
    {
        propertie = new Properties();
        try {
            inputFile = new FileInputStream(filePath);
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Fail---path is invalid");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("load file failed");
            ex.printStackTrace();
        }
    }//end ReadConfigInfo(...)
    
 
    public String getValue(String key)
    {
        if(propertie.containsKey(key)){
            String value = propertie.getProperty(key);
            return value;
        }
        else 
            return null;
    }//end getValue(...)
    
   
    public String getValue(String fileName, String key)
    {
        try {
            String value = "";
            inputFile = new FileInputStream(fileName);
            propertie.load(inputFile);
            inputFile.close();
            if(propertie.containsKey(key)){
                value = propertie.getProperty(key);
                return value;
            }else
                return value;
        } catch (FileNotFoundException e){
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
    }//end getValue(...)
    
    
    public void clear()
    {
        propertie.clear();
    }//end clear();
    
    
    public void setValue(String key, String value)
    {
        propertie.setProperty(key, value);
    }//end setValue(...)
    
    
    public void saveFile(String fileName, String description)
    {
        try{
            outputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    }//end saveFile(...)
    
    
}//end class ReadConfigInfo


